home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DataOutputStream.java < prev    next >
Text File  |  1998-09-22  |  11KB  |  335 lines

  1. /*
  2.  * @(#)DataOutputStream.java    1.24 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A data input stream lets an application write primitive Java data 
  19.  * types to an output stream in a portable way. An application can 
  20.  * then use a data input stream to read the data back in. 
  21.  *
  22.  * @author  unascribed
  23.  * @version 1.24, 07/01/98
  24.  * @see     java.io.DataInputStream
  25.  * @since   JDK1.0
  26.  */
  27. public
  28. class DataOutputStream extends FilterOutputStream implements DataOutput {
  29.     /**
  30.      * The number of bytes written to the data output stream. 
  31.      *
  32.      * @since   JDK1.0
  33.      */
  34.     protected int written;
  35.  
  36.     /**
  37.      * Creates a new data output stream to write data to the specified 
  38.      * underlying output stream. 
  39.      *
  40.      * @param   out   the underlying output stream.
  41.      * @see     java.io.FilterOutputStream#out
  42.      * @since   JDK1.0
  43.      */
  44.     public DataOutputStream(OutputStream out) {
  45.     super(out);
  46.     }
  47.  
  48.     /**
  49.      * Writes the specified byte to the underlying output stream. 
  50.      *
  51.      * @param      b   the <code>byte</code> to be written.
  52.      * @exception  IOException  if an I/O error occurs.
  53.      * @see        java.io.FilterOutputStream#out
  54.      * @since      JDK1.0
  55.      */
  56.     public synchronized void write(int b) throws IOException {
  57.     out.write(b);
  58.     written++;
  59.     }
  60.  
  61.     /**
  62.      * Writes <code>len</code> bytes from the specified byte array 
  63.      * starting at offset <code>off</code> to the underlying output stream.
  64.      *
  65.      * @param      b     the data.
  66.      * @param      off   the start offset in the data.
  67.      * @param      len   the number of bytes to write.
  68.      * @exception  IOException  if an I/O error occurs.
  69.      * @see        java.io.FilterOutputStream#out
  70.      * @since      JDK1.0
  71.      */
  72.     public synchronized void write(byte b[], int off, int len)
  73.     throws IOException
  74.     {
  75.     out.write(b, off, len);
  76.     written += len;
  77.     }
  78.  
  79.     /**
  80.      * Flushes this data output stream. This forces any buffered output 
  81.      * bytes to be written out to the stream. 
  82.      * <p>
  83.      * The <code>flush</code> method of <code>DataOuputStream</code> 
  84.      * calls the <code>flush</code> method of its underlying output stream.
  85.      *
  86.      * @exception  IOException  if an I/O error occurs.
  87.      * @see        java.io.FilterOutputStream#out
  88.      * @see        java.io.OutputStream#flush()
  89.      * @since      JDK1.0
  90.      */
  91.     public void flush() throws IOException {
  92.     out.flush();
  93.     }
  94.  
  95.     /**
  96.      * Writes a <code>boolean</code> to the underlying output stream as 
  97.      * a 1-byte value. The value <code>true</code> is written out as the 
  98.      * value <code>(byte)1</code>; the value <code>false</code> is 
  99.      * written out as the value <code>(byte)0</code>.
  100.      *
  101.      * @param      v   a <code>boolean</code> value to be written.
  102.      * @exception  IOException  if an I/O error occurs.
  103.      * @see        java.io.FilterOutputStream#out
  104.      * @since      JDK1.0
  105.      */
  106.     public final void writeBoolean(boolean v) throws IOException {
  107.     out.write(v ? 1 : 0);
  108.     written++;
  109.     }
  110.  
  111.     /**
  112.      * Writes out a <code>byte</code> to the underlying output stream as 
  113.      * a 1-byte value. 
  114.      *
  115.      * @param      v   a <code>byte</code> value to be written.
  116.      * @exception  IOException  if an I/O error occurs.
  117.      * @see        java.io.FilterOutputStream#out
  118.      * @since      JDK1.0
  119.      */
  120.     public final void writeByte(int v) throws IOException {
  121.     out.write(v);
  122.     written++;
  123.     }
  124.  
  125.     /**
  126.      * Writes a <code>short</code> to the underlying output stream as two
  127.      * bytes, high byte first. 
  128.      *
  129.      * @param      v   a <code>short</code> to be written.
  130.      * @exception  IOException  if an I/O error occurs.
  131.      * @see        java.io.FilterOutputStream#out
  132.      * @since      JDK1.0
  133.      */
  134.     public final void writeShort(int v) throws IOException {
  135.     OutputStream out = this.out;
  136.     out.write((v >>> 8) & 0xFF);
  137.     out.write((v >>> 0) & 0xFF);
  138.     written += 2;
  139.     }
  140.  
  141.     /**
  142.      * Writes a <code>char</code> to the underlying output stream as a 
  143.      * 2-byte value, high byte first. 
  144.      *
  145.      * @param      v   a <code>char</code> value to be written.
  146.      * @exception  IOException  if an I/O error occurs.
  147.      * @see        java.io.FilterOutputStream#out
  148.      * @since      JDK1.0
  149.      */
  150.     public final void writeChar(int v) throws IOException {
  151.     OutputStream out = this.out;
  152.     out.write((v >>> 8) & 0xFF);
  153.     out.write((v >>> 0) & 0xFF);
  154.     written += 2;
  155.     }
  156.  
  157.     /**
  158.      * Writes an <code>int</code> to the underlying output stream as four
  159.      * bytes, high byte first. 
  160.      *
  161.      * @param      v   an <code>int</code> to be written.
  162.      * @exception  IOException  if an I/O error occurs.
  163.      * @see        java.io.FilterOutputStream#out
  164.      * @since      JDK1.0
  165.      */
  166.     public final void writeInt(int v) throws IOException {
  167.     OutputStream out = this.out;
  168.     out.write((v >>> 24) & 0xFF);
  169.     out.write((v >>> 16) & 0xFF);
  170.     out.write((v >>>  8) & 0xFF);
  171.     out.write((v >>>  0) & 0xFF);
  172.     written += 4;
  173.     }
  174.  
  175.     /**
  176.      * Writes a <code>long</code> to the underlying output stream as eight
  177.      * bytes, high byte first. 
  178.      *
  179.      * @param      v   a <code>long</code> to be written.
  180.      * @exception  IOException  if an I/O error occurs.
  181.      * @see        java.io.FilterOutputStream#out
  182.      * @since      JDK1.0
  183.      */
  184.     public final void writeLong(long v) throws IOException {
  185.     OutputStream out = this.out;
  186.     out.write((int)(v >>> 56) & 0xFF);
  187.     out.write((int)(v >>> 48) & 0xFF);
  188.     out.write((int)(v >>> 40) & 0xFF);
  189.     out.write((int)(v >>> 32) & 0xFF);
  190.     out.write((int)(v >>> 24) & 0xFF);
  191.     out.write((int)(v >>> 16) & 0xFF);
  192.     out.write((int)(v >>>  8) & 0xFF);
  193.     out.write((int)(v >>>  0) & 0xFF);
  194.     written += 8;
  195.     }
  196.  
  197.     /**
  198.      * Converts the float argument to an <code>int</code> using the 
  199.      * <code>floatToIntBits</code> method in class <code>Float</code>, 
  200.      * and then writes that <code>int</code> value to the underlying 
  201.      * output stream as a 4-byte quantity, high byte first. 
  202.      *
  203.      * @param      v   a <code>float</code> value to be written.
  204.      * @exception  IOException  if an I/O error occurs.
  205.      * @see        java.io.FilterOutputStream#out
  206.      * @see        java.lang.Float#floatToIntBits(float)
  207.      * @since      JDK1.0
  208.      */
  209.     public final void writeFloat(float v) throws IOException {
  210.     writeInt(Float.floatToIntBits(v));
  211.     }
  212.  
  213.     /**
  214.      * Converts the double argument to a <code>long</code> using the 
  215.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  216.      * and then writes that <code>long</code> value to the underlying 
  217.      * output stream as an 8-byte quantity, high byte first. 
  218.      *
  219.      * @param      v   a <code>double</code> value to be written.
  220.      * @exception  IOException  if an I/O error occurs.
  221.      * @see        java.io.FilterOutputStream#out
  222.      * @see        java.lang.Double#doubleToLongBits(double)
  223.      * @since      JDK1.0
  224.      */
  225.     public final void writeDouble(double v) throws IOException {
  226.     writeLong(Double.doubleToLongBits(v));
  227.     }
  228.  
  229.     /**
  230.      * Writes out the string to the underlying output stream as a 
  231.      * sequence of bytes. Each character in the string is written out, in 
  232.      * sequence, by discarding its high eight bits. 
  233.      *
  234.      * @param      s   a string of bytes to be written.
  235.      * @exception  IOException  if an I/O error occurs.
  236.      * @see        java.io.FilterOutputStream#out
  237.      * @since      JDK1.0
  238.      */
  239.     public final void writeBytes(String s) throws IOException {
  240.     OutputStream out = this.out;
  241.     int len = s.length();
  242.     for (int i = 0 ; i < len ; i++) {
  243.         out.write((byte)s.charAt(i));
  244.     }
  245.     written += len;
  246.     }
  247.  
  248.     /**
  249.      * Writes a string to the underlying output stream as a sequence of 
  250.      * characters. Each character is written to the data output stream as 
  251.      * if by the <code>writeChar</code> method. 
  252.      *
  253.      * @param      s   a <code>String</code> value to be written.
  254.      * @exception  IOException  if an I/O error occurs.
  255.      * @see        java.io.DataOutputStream#writeChar(int)
  256.      * @see        java.io.FilterOutputStream#out
  257.      * @since      JDK1.0
  258.      */
  259.     public final void writeChars(String s) throws IOException {
  260.     OutputStream out = this.out;
  261.     int len = s.length();
  262.     for (int i = 0 ; i < len ; i++) {
  263.         int v = s.charAt(i);
  264.         out.write((v >>> 8) & 0xFF);
  265.         out.write((v >>> 0) & 0xFF);
  266.     }
  267.     written += len * 2;
  268.     }
  269.  
  270.     /**
  271.      * Writes a string to the underlying output stream using UTF-8 
  272.      * encoding in a machine-independent manner. 
  273.      * <p>
  274.      * First, two bytes are written to the output stream as if by the 
  275.      * <code>writeShort</code> method giving the number of bytes to 
  276.      * follow. This value is the number of bytes actually written out, 
  277.      * not the length of the string. Following the length, each character 
  278.      * of the string is output, in sequence, using the UTF-8 encoding 
  279.      * for the character. 
  280.      *
  281.      * @param      str   a string to be written.
  282.      * @exception  IOException  if an I/O error occurs.
  283.      * @since      JDK1.0
  284.      */
  285.     public final void writeUTF(String str) throws IOException {
  286.     OutputStream out = this.out;
  287.     int strlen = str.length();
  288.     int utflen = 0;
  289.  
  290.     for (int i = 0 ; i < strlen ; i++) {
  291.         int c = str.charAt(i);
  292.         if ((c >= 0x0001) && (c <= 0x007F)) {
  293.         utflen++;
  294.         } else if (c > 0x07FF) {
  295.         utflen += 3;
  296.         } else {
  297.         utflen += 2;
  298.         }
  299.     }
  300.  
  301.     if (utflen > 65535)
  302.         throw new UTFDataFormatException();          
  303.  
  304.     out.write((utflen >>> 8) & 0xFF);
  305.     out.write((utflen >>> 0) & 0xFF);
  306.     for (int i = 0 ; i < strlen ; i++) {
  307.         int c = str.charAt(i);
  308.         if ((c >= 0x0001) && (c <= 0x007F)) {
  309.         out.write(c);
  310.         } else if (c > 0x07FF) {
  311.         out.write(0xE0 | ((c >> 12) & 0x0F));
  312.         out.write(0x80 | ((c >>  6) & 0x3F));
  313.         out.write(0x80 | ((c >>  0) & 0x3F));
  314.         written += 2;
  315.         } else {
  316.         out.write(0xC0 | ((c >>  6) & 0x1F));
  317.         out.write(0x80 | ((c >>  0) & 0x3F));
  318.         written += 1;
  319.         }
  320.     }
  321.     written += strlen + 2;
  322.     }
  323.  
  324.     /**
  325.      * Returns the number of bytes written to this data output stream.
  326.      *
  327.      * @return  the value of the <code>written</code> field.
  328.      * @see     java.io.DataOutputStream#written
  329.      * @since   JDK1.0
  330.      */
  331.     public final int size() {
  332.     return written;
  333.     }
  334. }
  335.